<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        ul {
            display: flex;
            width: 500px;
            height: 300px;
            background-image: url("./img/gang.png");
            background-size: 100% 100%;
            border: 1px solid #000;
            margin: 80px auto;
        }

        li {
            width: 100px;
            /* border: 1px solid rgb(245, 10, 10); */

        }

        .action {
            background-image: linear-gradient(rgba(255, 0, 0, 0), rgba(23, 22, 22, 0.647) 120%);
        }
    </style>
</head>

<body>

    <div></div>
    <ul>

    </ul>
    <script>
        var str = "", str1 = "";
        for (var i = 1; i <= 9; i++) {
            str += `<audio src="./notes/${i}.oga"></audio>`;
            str1 += "<li></li>"
        }
        var div = document.querySelector("div");
        var ul = document.querySelector("ul");

        div.innerHTML = str;
        ul.innerHTML = str1;
        var as = document.querySelectorAll("audio");
        var lis = document.querySelectorAll("li");
        
        for (let m = 0; m < lis.length; m++) {
            lis[m].onmousedown = function () {
                as[m].load();
                as[m].play();
                this.classList.add("action");
            }
            lis[m].onmouseup = function () {
                this.classList.remove("action");
            }
        }

        // 添加键盘事件
        // 假设按1-9 执行音频播放
        window.onkeydown = function (e) {
            if (e.keyCode >= 49 && e.keyCode <= 57) {
                // console.log(e.keyCode - 49);
                // 49  0
                // 50  1
                // 51  2
                var m = e.keyCode - 49;
                as[m].load();
                as[m].play();
                lis[m].classList.add("action");
            }
        }
        window.onkeyup = function (e) {
            if (e.keyCode >= 49 && e.keyCode <= 57) {
                var m = e.keyCode - 49;
                lis[m].classList.remove("action");
            }
        }
    </script>


</body>

</html> 